<?php

/**
 * Model_MoteGateway
 *
 */
  
class Model_MoteGateway
{
    const CMD_UPDATE ='UPDATE_TEMP';
    const CMD_SETMODE='SET_MODE';
    
    
    const MODE_HEAT=0;
    const MODE_COOL=1;
    
    //const MOTECOM="/Users/scj/Sites/ac2/MoteInterface/motecom.txt";
    const MOTECOM='motecom.txt';
        
    //const MOTE_SENDER='java -jar /Users/scj/Sites/ac2/MoteInterface/PacketSender.jar';
    const MOTE_SENDER='PacketSender.jar';
    
/*
  java -jar PacketListener.jar <mote address> <command> <argument> <node_id>
  Commands are:
      UPDATE_TEMP <temp in F> <target zone>
      REQUEST_ZONE_STATUS 0 <target zone>
      SET_MODE <mode> <target zone>
*/    

//    const MOTE_LISTENER='java -jar /Users/scj/Sites/ac2/MoteInterface/PacketListener.jar';
        
    protected $setpoint;
    protected $motecom; //mote COM port
    protected $motesender;
    

    protected function _getMoteCom() 
    {
      if (null === $this->motecom) {
        $f = fopen(APPLICATION_PATH . '/../MoteInterface/' . Model_MoteGateway::MOTECOM, "r");
        $this->motecom = trim(fgets($f, 1000));
      }
      return $this->motecom;
    }

    protected function _getMoteSender() 
    {
      if (null === $this->motesender) {
        $this->motesender = 'java -jar ' . APPLICATION_PATH . '/../MoteInterface/' . Model_MoteGateway::MOTE_SENDER;
      }
      return $this->motesender;
    }
        
    
    protected function _getSetpointModel()
    {
        if (null === $this->setpoint) {
            require_once APPLICATION_PATH . '/models/Setpoint.php';
            $this->setpoint = new Model_Setpoint();
        }
        return $this->setpoint;
    }
    
    
    public function updateSetpoint($room_id, $setpoint) {
      $zone_id  = $this->_getSetpointModel()->fetchZones($room_id);   // zone_id == node_id
      $port = $this->_getMoteCom();
      $cmd  = $this->_getMoteSender() . ' ' . $port . ' ' .
              Model_MoteGateway::CMD_UPDATE  . ' ' . $setpoint . ' ' . $zone_id;

      //echo shell_exec('whoami');

      //
      // TBD - we should log this 
      //
      //var_dump($cmd);
      $output = shell_exec($cmd);
      //var_dump($output);
      //exit;
    }

    public function setMode($zone_id, $mode) {
      // zone_id == node_id
      $port = $this->_getMoteCom();
      $cmd  = $this->_getMoteSender() . ' ' . $port . ' ' .
              Model_MoteGateway::CMD_SETMODE . ' ' . $mode . ' ' . $zone_id;
      //
      // TBD - we should log this 
      //
      //var_dump($cmd);
      $output = shell_exec($cmd);
      //var_dump($this->_getMoteCom());
      //var_dump($output);
      //exit;
    }

/*
// Couldn't not get his to work
// Launching process in background messed stuff up for some reason
    public function getReadings()
    {
      $port = $this->_getMoteCom();
      $cmd = Model_MoteGateway::MOTE_LISTENER . ' ' . $port .'>/dev/null &#038;';
//      var_dump($cmd);
      $output = shell_exec($cmd);
      
//      var_dump($output);
//     exit;
    }
*/
}